package com.minelx.table.core;

import com.minelx.table.core.series.Series;
import com.minelx.table.core.series.index.BoolSeriesIndexes;
import com.minelx.table.core.series.index.Indexes;

public class RowBoolIndexedTableProvider implements TableProvider {

	private final Table decorated;

	private final Series<Boolean> booleanIndexes;

	private final Indexes rowIndexes;

	private final Indexes columnIndexes;

	public RowBoolIndexedTableProvider(Table decorated,
	                                   Series<Boolean> booleanIndexes) {
		this.decorated = decorated;
		this.booleanIndexes = booleanIndexes;

		rowIndexes = BoolSeriesIndexes.create(booleanIndexes.indexes(), booleanIndexes);
		columnIndexes = decorated.form().columnIndexes();
	}

	@Override
	public Series<String> row(long at) {
		if (booleanIndexes.at(at)) {
			return decorated.form().row(at);
		}
		throw new UnsupportedOperationException("operation is unsupported.");
	}

	@Override
	public Series<String> column(long at) {
		return decorated.form().column(at).mapIndexes(indexes -> BoolSeriesIndexes.create(indexes, booleanIndexes));
	}

	@Override
	public Series<String> title() {
		return decorated.form().title();
	}

	@Override
	public Indexes rowIndexes() {
		return rowIndexes;
	}

	@Override
	public Indexes columnIndexes() {
		return columnIndexes;
	}
}
